package com.hengtongxing.hejiayun.life;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.Adapter_ViewPager_Fragment;
import com.hengtongxing.hejiayun.base.BaseActivity;
import com.hengtongxing.hejiayun.base.BaseFragment;
import com.hengtongxing.hejiayun.widget.ChildColorTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商品列表
 */
public class GoodsListActivity extends BaseActivity {

    @BindView(R.id.view_leftIcon)
    View viewLeftIcon;
    @BindView(R.id.ll_left)
    LinearLayout llLeft;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.view_cancel)
    View viewCancel;
    private List<String> mDataList;
    private Adapter_ViewPager_Fragment adapter_viewPager;
    private int selectPosition;
    private String keyword = "";
    private String query_str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        selectPosition = getIntent().getIntExtra("selectPosition", 0);
        query_str = getIntent().getStringExtra("query_str");
        mDataList = new ArrayList<>();
        mDataList.add("综合推荐");
        mDataList.add("销量");
        mDataList.add("价格");
        //ViewPager的适配器
        List<BaseFragment> fragmentList = new ArrayList<>();
        for (String title : mDataList) {
            GoodsListFragment fragment = new GoodsListFragment();
            Bundle arguments = new Bundle();
            arguments.putString(GoodsListFragment.ORDER_TITLE_NAME, title);
            arguments.putString(GoodsListFragment.ORDER_TITLE_QUERY, query_str);
            fragment.setArguments(arguments);
            fragmentList.add(fragment);
        }
        adapter_viewPager = new Adapter_ViewPager_Fragment(getSupportFragmentManager(), fragmentList, mDataList);
        viewPager.setAdapter(adapter_viewPager);
        //指示器适配
        initMagicIndicator();

        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if (!TextUtils.isEmpty(keyword)) {
//                        refreshData();
                    }
                    return true;
                }
                return false;
            }
        });
        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                keyword = editSearch.getText().toString().trim();
                if (!TextUtils.isEmpty(keyword)) {
                    viewCancel.setVisibility(View.VISIBLE);
                } else {
                    viewCancel.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void initMagicIndicator() {
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(mActivity);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ChildColorTitleView simplePagerTitleView = new ChildColorTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setNormalColor(Color.parseColor("#242424"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#242424"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                indicator.setLineWidth(UIUtil.dip2px(context, 24));
                indicator.setColors(Color.parseColor("#44C75E"));
                return indicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
        viewPager.setOffscreenPageLimit(5);
        viewPager.setCurrentItem(selectPosition);
    }

    @OnClick(R.id.ll_left)
    public void onViewClicked() {
        finish();
    }
}
